package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.entity.banquet.UserBanquetSetting;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.banquet.BanquetModule;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

//@Cmd(code = GameProtocol.S_PARTY_OPEN, desc = "")
@Cmd(code = Protocol.S_BANQUET_USER_SETTING_UPDATE, desc = "更新宴会设置数据")
public class UpdateBanquetSettingCmd extends AbstractCommand {
    private int type;
    private String giftTypeAccept;

    private String message;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        BanquetProto.UpdateBanquetSettingMsg reqMsg = BanquetProto.UpdateBanquetSettingMsg.parseFrom(packet.getBytes());
        this.type = reqMsg.getType();
        this.giftTypeAccept = reqMsg.getGiftTypeAccept();
        if (null == this.giftTypeAccept) {
            this.giftTypeAccept = "";
        }

        this.message = reqMsg.getMessage();
        if (message == null) {
            this.message = "";
        }

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (!StringUtils.isNullOrEmpty(this.giftTypeAccept)) {
            List<Integer> giftIdList = new ArrayList<>();
            String[] giftTypes =  this.giftTypeAccept.split("\\|");
            for (String giftType : giftTypes) {
                if (!StringUtils.isNumber(giftType)) {
                    ret = GameErrorCode.E_BANQUET_SETTING_GIFTTYPE_WRONG;
                    return;
                }

                int giftId = Integer.parseInt(giftType);

                if (null == BanquetConfigMgr.getBanquetGiftInfo(this.type, giftId)) {
                    ret = GameErrorCode.E_BANQUET_SETTING_GIFTTYPE_WRONG;
                    return;
                }

                if (!giftIdList.contains(giftId)) {
                    giftIdList.add(giftId);
                }
            }
            this.giftTypeAccept = StringUtils.listToString(giftIdList,"|");
        }

        if (BanquetConfigMgr.isServerNormalBanquet(type) && !SystemOpenMgr.systemOpen(player, eSystemId.Banquet.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if (BanquetConfigMgr.isServerFeastBanquet(type) && !SystemOpenMgr.systemOpen(player, eSystemId.BanquetServer.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(type);
        if (banquetInfo == null) {
            ret = GameErrorCode.E_BANQUET_INFO_ERROR;
            return;
        }

        BanquetProto.UpdateBanquetSettingReqMsg.Builder msg = BanquetProto.UpdateBanquetSettingReqMsg.newBuilder();
        msg.setType(type);
        msg.setGiftTypeAccept(this.giftTypeAccept);
        msg.setBanquetId(player.getModule(BanquetModule.class).getUserBanquetSettingBanquetId(type));
        msg.setMessage(this.message);
        //发送到跨服更新进行中的宴会的设置
        player.sendPacket(CrossProtocol.C_CROSS_UPDATE_PARTY_SETTING, msg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
